﻿Imports System.Runtime.InteropServices

Namespace Widgets

    Delegate Sub XboxInfo_UpdateDelegate(ByVal r As DMXIProxy.XboxInfo)

    Public Class XboxInfoRetreval(Of TView As {IXboxInfoRetrevalView, Control})
        Inherits WidgetBase(Of TView)

        Private m_Timer As New System.Timers.Timer(360000)

        Friend Event Update(ByVal r As DMXIProxy.XboxInfo)

        Public Sub New(ByVal View As TView)
            MyBase.New(View)
            AddHandler m_Timer.Elapsed, AddressOf m_Timer_Elapsed
            m_Timer.Start()
        End Sub

        Public Sub Start()
            m_Timer = New System.Timers.Timer(New TimeSpan(0, My.Settings.MinutesBetweenChecks, 0).Ticks)
            m_Timer.Start()
        End Sub

        Public Sub [Stop]()
            m_Timer.Stop()
            ' View.OnTaskTimer_Stop(m_TimeSpan)
        End Sub

        Private Sub m_Timer_Elapsed()
            CheckXboxLiveStatus()
        End Sub

        Friend Sub CheckXboxLiveStatus()
            Dim c As New DMXIProxy.XboxInfoSoapClient
            Dim r As DMXIProxy.XboxInfo
            r = c.GetXboxInfo(View.GetGamerTag)
            If Not View.LastUpdateValue = r.PresenceInfo.Info Then
                View.LastUpdateValue = r.PresenceInfo.Info
                ' TODO: Check to see if data has realy changed if not, don't fire events
                UpdateFromTimers(r)
            End If
        End Sub

        Private Sub UpdateFromTimers(ByVal r As DMXIProxy.XboxInfo)
            If View.InvokeRequired Then
                View.Invoke(New XboxInfo_UpdateDelegate(AddressOf UpdateFromTimers), r)
            Else
                RaiseEvent Update(r)
            End If
        End Sub

    End Class

End Namespace